rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)
library(ggplot2)

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, memrableperiod_pret := memrableperiod_pret * 100]
m[, memrableperiod_aret := memrableperiod_aret * 100]
m[, memrableperiod_ownret := memrableperiod_ownret * 100]
m[, retrecall_1day_rate := retrecall_1day_rate * 100]
m[, retrecall_30day_rate := retrecall_30day_rate * 100]
m[, retrecall_1year_rate := retrecall_1year_rate * 100]
m[, retrecall_5year_rate := retrecall_5year_rate * 100]

m[, expret30day_market_rate := expret30day_market_rate * 100]
m[, expret30day_self_rate := expret30day_self_rate * 100]
m[, expret1year_market_rate := expret1year_market_rate * 100]
m[, expret1year_self_rate := expret1year_self_rate * 100]

f1 <- cor.test(m$memrableperiod_aret, m$memrableperiod_pret, use = "pairwise.complete.obs")

C <- data.table(read_xlsx("../Data/AccountData.xlsx", guess_max = 1e4))
C[, aret_1day_v2 := as.numeric(aret_1day_v2) * 100]
C[, aret_0day_v2 := as.numeric(aret_0day_v2) * 100]
C[, aret_4_1week_v2 := as.numeric(aret_4_1week_v2) * 100]
C[, aret_ytd_v2 := as.numeric(aret_ytd_v2) * 100]
m <- merge(m, C[, .(id, aret_1day_v2, aret_0day_v2, aret_4_1week_v2, aret_ytd_v2)],
  by = "id", all.x = T
)

m[, aret_1day_v2 := Winsorize(aret_1day_v2, quantile(aret_1day_v2, probs = c(0.05, 0.95), na.rm = T))]
m[, aret_0day_v2 := Winsorize(aret_0day_v2, quantile(aret_0day_v2, probs = c(0.05, 0.95), na.rm = T))]
m[, aret_4_1week_v2 := Winsorize(aret_4_1week_v2, quantile(aret_4_1week_v2, probs = c(0.05, 0.95), na.rm = T))]
m[, aret_ytd_v2 := Winsorize(aret_ytd_v2, quantile(aret_ytd_v2, probs = c(0.05, 0.95), na.rm = T))]

f2 <- cor.test(m$aret_1day_v2, m$retrecall_1day_rate, use = "pairwise.complete.obs")
f3 <- cor.test(m$aret_4_1week_v2, m$retrecall_30day_rate, use = "pairwise.complete.obs")
f4 <- cor.test(m$aret_ytd_v2, m$retrecall_1year_rate, use = "pairwise.complete.obs")

# Table 3: correlation, s.e., p values
sprintf("%.2f, %.2f, %.2f", f1$estimate, sqrt((1 - f1$estimate^2) / (f1$parameter)), f1$p.value)
sprintf("%.2f, %.2f, %.2f", f2$estimate, sqrt((1 - f2$estimate^2) / (f2$parameter)), f2$p.value)
sprintf("%.2f, %.2f, %.2f", f3$estimate, sqrt((1 - f3$estimate^2) / (f3$parameter)), f3$p.value)
sprintf("%.2f, %.2f, %.2f", f4$estimate, sqrt((1 - f4$estimate^2) / (f4$parameter)), f4$p.value)
